# coding=utf8
import sys
import re
if sys.platform == 'win32':
    from twisted.internet import win32eventreactor
    win32eventreactor.install()
from twisted.internet import defer, threads
from twisted.internet.serialport import SerialPort
from twisted.internet import protocol,task, reactor, error
from twisted.protocols.basic import LineReceiver
from twisted.python import log, usage
class GasSim(LineReceiver):
    def __init__(self):
        self._lc1 = task.LoopingCall(self.sendCmd)
        self._lc1.start(3,False)
    def sendCmd(self):
        self.sendLine("@01c100001".encode('utf-8'))
    def lineReceived(self, line):
        line = line.decode('utf-8')
        print(line)
        if(re.match('@\d{2}\D\w{4}\d{2}',line)):
            if(line[3] == 'D'):
                print (u'接收到1条命令！\r\n')
                tmpstr = "当前电压值=%.3f" % (int(line[4:8],16)/1000.0)
                print (tmpstr)
if __name__ == '__main__':
    s = SerialPort(GasSim(), 'COM2', reactor, baudrate=9600)
    reactor.run()